package com.hk.service.yingmi.impl;

import com.hk.commons.HqlFilter;
import com.hk.commons.entity.FundInfo_Yimi;
import com.hk.commons.entity.yingmi.YingmiApiEvent;
import com.hk.commons.entity.yingmi.YingmiBrokerProduct;
import com.hk.dao.base.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Service
public class YingmiBrokerProductRepository extends BaseServiceImpl<YingmiBrokerProduct> {


    public YingmiBrokerProduct store(YingmiBrokerProduct product) {

        YingmiBrokerProduct findExist = findExistingFund(product.getFundCode());

        if(findExist == null){
            this.save(product);
            findExist = product;
        }

        if(findExist != null){

            findExist.setRisk5Level(product.getRisk5Level());
            findExist.setDurationDesc(product.getDurationDesc());
            findExist.setExpectedReturnDesc(product.getExpectedReturnDesc());
            findExist.setAmacRisk5Level(product.getAmacRisk5Level());
            findExist.setMinIndividualSubscribeAmount(product.getMinIndividualSubscribeAmount());

            findExist.setSubscribeOpenDate(product.getSubscribeOpenDate());
            findExist.setAllotOpenDate(product.getAllotOpenDate());
            findExist.setRedeemOpenDate(product.getRedeemOpenDate());

            findExist.setLastUpdated(new Date());

            this.update(findExist);
        }

        return findExist;
    }

    private YingmiBrokerProduct findExistingFund(String fundCode) {
        HqlFilter hqlFilter = new HqlFilter();
        hqlFilter.addEQFilter("fundCode", fundCode);
        List<YingmiBrokerProduct> list= this.findByFilter(hqlFilter);

        if(list == null || list.size() == 0)
            return null;

        return list.get(0);
    }
}
